const Router = require('koa-router')
const { resDesign } = require('@/utils/dataDesign')
const SystemService = require('@/service/system.service')
const HTTP_CODE = require('@/app/http-code')

const systemRouter = new Router({ prefix: '/system/menu' })

// 查询菜单列表
systemRouter.get('/list', async (ctx) => {
  try {
    const data = await SystemService.selectMenus(ctx.query)
    ctx.body = resDesign({ data })
  } catch (error) {
    console.log(error)
  }
})
// 新建菜单
systemRouter.post('/insert', async (ctx) => {
  try {
    await SystemService.insertMenu(ctx.request.body)
    ctx.body = resDesign()
  } catch (error) {
    console.log(error)
  }
})
// 编辑菜单
systemRouter.patch('/update', async (ctx) => {
  try {
    await SystemService.updateMenu(ctx.request.body)
    ctx.body = resDesign()
  } catch (error) {
    console.log(error)
  }
})
// 删除菜单
systemRouter.delete('/delete/:id', async (ctx) => {
  try {
    const { id } = ctx.params
    const flag = await SystemService.deleteMenu(id)
    if (!flag) {
      ctx.body = resDesign({ code: HTTP_CODE.error, msg: '请先清空子菜单！' })
    } else {
      ctx.body = resDesign()
    }
  } catch (error) {
    console.log(error)
  }
})

module.exports = systemRouter
